package com.situ.library;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Scanner;


public class LibraryMangement {
	Scanner sc = new Scanner(System.in);
	BookDao bd = new BookDao();
	CardDao cd = new CardDao();
	RecordDao rd = new RecordDao();

	public static void main(String[] args) {
		LibraryMangement lm = new LibraryMangement();
		lm.start();
	}

	public void start() {
		while (true) {
			System.out.println("---图书管理系统---");
			System.out.println("1.进书");
			System.out.println("2.借书卡办理");
			System.out.println("3.借书");
			System.out.println("4.还书");
			System.out.println("5.查询某书的借出记录");
			System.out.println("6.查询借书卡的借出记录");
			System.out.println("7.显示图书列表");
			System.out.println("8.退出系统");
			System.out.println("请输入：");
			switch (Integer.parseInt(sc.nextLine())) {
			case 1:
				storeBooks();
				break;
			case 2:
				manageBookCard();
				break;
			case 3:
				borrowBook();
				break;
			case 4:
				returnBook();
				break;
			case 5:
				findRecordOfBook();
				break;
			case 6:
				findRecord();
				break;
			case 7:
				showBook();
				break;
			case 8:
				sc.close();
				return;
			default:
				System.out.println("输入有误，请重新输入！！");
				break;
			}

		}
	}

	/**
	 * 书店进书
	 */
	public void storeBooks() {

		while (true) {
			System.out.println("------进书------");
			System.out.println("请输入图书编号：");
			String Id = sc.nextLine();
			BookModel book = isExistId(Id);
			if (isExistId(Id) != null) {
				System.out.println("已存在图书ID,书名为 " + book.getName() + ",是否继续(y/n):");
				String in = sc.nextLine();
				if (in.equalsIgnoreCase("n")) {
					continue;
				}
				if (in.equalsIgnoreCase("y")) {
					System.out.println("请输入添加的数量:");
					int num = Integer.parseInt(sc.nextLine());
					bd.update(new BookModel(book.getId(), book.getName(), num + book.getNumber(), book.getCount()));
					System.out.println("添加成功，是否继续添加(y/n):");
					String in1 = sc.nextLine();
					if (in1.equalsIgnoreCase("y"))
						continue;
					if (in1.equalsIgnoreCase("n"))
						break;
				}
			}
			System.out.println("请输入书名：");
			String bname = sc.nextLine();
			System.out.println("请输入进几本书：");
			int bnum = Integer.parseInt(sc.nextLine());

			BookModel bm = new BookModel(Id, bname, bnum, 0);
			bd.insert(bm);

			System.out.println("是否继续添加(y/n)：");
			if (sc.nextLine().equalsIgnoreCase("n")) {
				break;
			}
		}
	}

	/**
	 * 进书子功能 -- 查找学生ID是否存在
	 * 
	 * @param Id
	 * @return
	 */
	public BookModel isExistId(String Id) {
		List<BookModel> list = bd.selectList(new BookModel(Id, null, null, null));
		for (BookModel book : list) {
			if (book.getId().equals(Id)) {
				return book;
			}
		}
		return null;
	}

	/**
	 * 进书子功能 -- 增加书的数量
	 * 
	 * @param book
	 * @return
	 */
	public boolean addNum(BookModel book) {
		System.out.println("书名为: " + book.getName());
		System.out.println("请输入添加的数量:");
		int num = Integer.parseInt(sc.nextLine());
		bd.update(new BookModel(book.getId(), book.getName(), book.getNumber() + num, book.getCount()));
		System.out.println("添加成功，是否继续添加(y/n):");
		if (sc.nextLine().equalsIgnoreCase("y")) {
			return true;
		}
		if (sc.nextLine().equalsIgnoreCase("n")) {
			return false;
		}
		return false;
	}

	/**
	 * 判断书籍是否存在
	 * 
	 * @param bname 输入书名
	 * @return 返回书籍
	 */
	public BookModel isExist(String bname) {
		List<BookModel> list = bd.selectList(new BookModel(null, bname, null, null));
		if (list != null) {
			for (BookModel bm : list) {
				if (bname.equals(bm.getName())) {
					return bm;
				}
			}
		}
		return null;
	}

	/**
	 * 借记卡办理
	 */
	public void manageBookCard() {
		Integer id = 0;
		while (true) {
			System.out.println("输入姓名：");
			String username = sc.nextLine();
			List<CardModel> list = cd.selectList(new CardModel(null, null));
			int len = list.size();
			CardModel cm = isExistCard(username);

			if (cm != null) {
				System.out.println("该借记卡已存在！");
			}
			if (cm == null) {
				id = len + 1;
				cm = new CardModel(id.toString(), username);
				if (cd.insert(cm) != 0)
					System.out.println("办理成功！您的借记卡ID为：" + id);
				else
					System.out.println("办理失败！！");
			}
			System.out.println("是否继续(y/n):");
			if (sc.nextLine().equalsIgnoreCase("n")) {
				break;
			}
		}
	}

	/**
	 * 查看是否已存在借记卡
	 * 
	 * @param cname
	 * @return 返回存在的借记卡或null
	 */
	public CardModel isExistCard(String cname) {
		List<CardModel> list = cd.selectList(new CardModel(null, null));
		for (CardModel cm : list) {
			if (cm.getUserName().equals(cname)) {
				return cm;
			}
		}
		return null;
	}

	/**
	 * 借书
	 */
	public void borrowBook() {
		List<BookModel> blist = bd.selectList(new BookModel(null, null, null, null));
		for (BookModel bm : blist) {
			System.out.println(bm.getName());
		}
		System.out.println("输入要借的图书名：");
		String bname = sc.nextLine();
		int val = findBook(bname);
		if (val == -1) {
			System.out.println("未存储图书！");
			return;
		}
		if (val == -2) {
			System.out.println("该图书没有剩余！");
			return;
		}
		if (val == 0) {
			System.out.println("未找到该图书！");
			return;
		}
		if (val == 1) {
			borrowCount(bname);
		}
	}

	/**
	 * 借阅图书子功能--图书是否存在
	 * 
	 * @param bname
	 * @return 1 存在 0 不存在 -1 不存在 2 存在但是数量不够
	 */
	public int findBook(String bname) {
		List<BookModel> blist = bd.selectList(new BookModel(null, bname, null, null));
		if (blist == null) {
			return -1;
		} else {
			for (BookModel bm : blist) {
				if (bname.equals(bm.getName())) {
					if (bm.getNumber() > 0) {
						return 1;
					} else {
						return -2;
					}
				}
			}
		}
		return 0;
	}

	/**
	 * 借书子功能-- 登记借记卡
	 * 
	 * @param bname
	 */
	public void borrowCount(String bname) {
		System.out.println("---需要登记借记卡---");
		System.out.println("请输入借记卡号：");
		String cid = sc.nextLine();
		CardModel cm = isCardIdExist(cid);
		if (cm != null) {
			System.out.println(cm.getUserName() + "的借书卡");
			AlterBookState(cm, bname);
		} else {
			System.out.println("未查找到借记卡，请输入您办理借记卡时的姓名：");
			String name = sc.nextLine();
			if (isCardNameExist(name) == null) { // 输入的姓名未找到
				System.out.println("您未办理借记卡，请返回办理！！");
				return;
			} else {
				System.out.println("您的借记卡号为：" + isCardNameExist(name).getId() + "请返回重新办理！");
			}

		}
	}

	/**
	 * 借书子功能 - 登记借记卡子功能 - 查看借记卡号是否存在
	 * 
	 * @param id
	 * @return 返回卡实例或者null
	 */
	public CardModel isCardIdExist(String id) {
		List<CardModel> clist = cd.selectList(new CardModel(id, null));
		for (CardModel cm : clist) {
			if (id.equals(cm.getId())) {
				return cm;
			}
		}
		return null;
	}

	/**
	 * 借书子功能 -- 查看姓名是否存在于借记卡中
	 * 
	 * @param name 输入姓名
	 * @return 返回借记卡或者null
	 */
	public CardModel isCardNameExist(String name) {
		List<CardModel> blist = cd.selectList(new CardModel(null, null));
		for (CardModel ncm : blist) {
			if (ncm.getUserName().equals(name)) {
				return ncm;
			}
		}
		return null;
	}

	/**
	 * 借书子功能 -- 修改借书记录
	 * 
	 * @param cm
	 * @param bname
	 */
	public void AlterBookState(CardModel cm, String bname) {
		System.out.println("允许借阅！");
		// 修改图书状态
		BookModel bc = null;
		List<BookModel> blist = bd.selectList(new BookModel(null, bname, null, null));
		for (BookModel bc1 : blist) { // 找到书名的对像
			if (bc1.getName().equals(bname)) {
				bc = bc1;
				break;
			}
		}
		bd.update(new BookModel(bc.getId(), bname, bc.getNumber() - 1, bc.getCount() + 1));
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String timeDate = sdf.format(new Date(0));
		RecordModel rm = new RecordModel(bc.getId(), cm.getId(), 1);
		rm.setTimeDate(timeDate);
		if (rd.insert(rm) != 0)
			System.out.println("借阅成功！");
		else
			System.out.println("借阅失败！！");
	}

	/**
	 * 还书
	 */
	public void returnBook() {
		List<BookModel> blist = bd.selectList(new BookModel(null, null, null, null));
		for (BookModel bm : blist) {
			System.out.println(bm.getName());
		}
		System.out.println("输入要还的图书名：");
		String bname = sc.nextLine();
		int val = findReBook(bname);
		if (val == 0) {
			System.out.println("未找到该图书！");
			return;
		}
		if (val == 1) {
			returnCount(bname);
		}
	}

	/**
	 * 还书子功能--要还的图书是否存在
	 * 
	 * @param bname
	 * @return 1 存在 0 不存在
	 */
	public int findReBook(String bname) {
		List<BookModel> blist = bd.selectList(new BookModel(null, bname, null, null));
		return blist != null ? 1 : 0;
	}

	/**
	 * 还书子功能-- 登记借记卡
	 * 
	 * @param bname
	 */
	public void returnCount(String bname) {
		System.out.println("---需要登记借记卡---");
		System.out.println("请输入借记卡号：");
		String cid = sc.nextLine();
		CardModel cm = isCardIdExist(cid);
		if (cm != null) {
			System.out.println(cm.getUserName() + "的借书卡");
			alterReBookState(cm, bname);
		} else {
			System.out.println("未查找到借记卡，请输入您办理借记卡时的姓名：");
			String name = sc.nextLine();
			if (isCardNameExist(name) == null) { // 输入的姓名未找到
				System.out.println("您未办理借记卡，请返回办理！！");
				return;
			} else {
				System.out.println("您的借记卡号为：" + isCardNameExist(name).getId() + "请返回重新办理！");
			}

		}
	}

	/**
	 * 还书子功能 -- 修改还书记录
	 * 
	 * @param cm
	 * @param bname
	 */
	public void alterReBookState(CardModel cm, String bname) {
		System.out.println("允许归还！");
		// 修改图书的状态
		BookModel bc = null;
		List<BookModel> blist = bd.selectList(new BookModel(null, bname, null, null));
		for (BookModel bc1 : blist) { // 找到书名的对像
			if (bc1.getName().equals(bname)) {
				bc = bc1;
				break;
			}
		}
		bd.update(new BookModel(bc.getId(), bname, bc.getNumber() + 1, bc.getCount()));
		// 增添还书记录
		// 设置还书时的日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String timeDate = sdf.format(new Date(0));
		RecordModel rm = new RecordModel(bc.getId(), cm.getId(), 0);
		rm.setTimeDate(timeDate);
		if (rd.insert(rm) != 0)
			System.out.println("还书成功！");
		else
			System.out.println("还书失败！！");
	}

	/**
	 * 找到图书的借出记录
	 */
	public void findRecordOfBook() {
		while (true) {
			System.out.println("请输入图书名:");
			String bname = sc.nextLine();
			List<RecordModel> list = rd.selectList(new RecordModel(null, null, null));
			if (list == null) {
				System.out.println("未找到借书卡里的信息");
				break;
			}
			// 根据图书名找到图书Id
			List<BookModel> blist = bd.selectList(new BookModel(null, bname, null, null));
			String bid = null;
			for (BookModel bm : blist) {
				if (bm.getName().equals(bname)) {
					bid = bm.getId();
					break;
				}
			}
			// 根据图书Id找到记录
			for (RecordModel record : list) {
				if (bid.equals(record.getBId())) {
					String type = decodeType(record.getType());
					System.out.println("书ID: " + record.getBId() + " 书名:" + bname + " 借书卡ID: " + record.getCId()
							+ " 类型:" + type + " 日期:" + record.getTimeDate());
				}
			}
			System.out.println("是否继续(y/n):");
			if (sc.nextLine().equalsIgnoreCase("n"))
				break;
		}
	}

	/**
	 * 查询借书卡的借出记录
	 */
	public void findRecord() {
		while (true) {
			System.out.println("---查询借书卡的借出记录---");
			System.out.println("1.查询所有借书卡");
			System.out.println("2.根据借书卡的Id查找借书卡的借书记录");
			int key = Integer.parseInt(sc.nextLine());
			switch (key) {
			case 1:
				outputCard();
				break;
			case 2:
				System.out.println("请输入借书卡ID:");
				outputCardData(sc.nextLine());
				break;
			default:
				System.out.println("输入有误！！");
				break;
			}
			System.out.println("是否继续(y/n):");
			if (sc.nextLine().equalsIgnoreCase("n")) {
				break;
			}
		}
	}

	/**
	 * 查询借书卡子功能 -- 输出全部借书卡
	 */
	public void outputCard() {
		List<CardModel> list1 = cd.selectList(new CardModel(null, null));
		if (list1 == null) {
			System.out.println("未找到借书卡信息");
			return;
		}
		for (CardModel book : list1) {
			System.out.println("ID: " + book.getId() + " 名: " + book.getUserName());
		}
	}

	/**
	 * 查询借书卡子功能 -- 输出借书卡里的信息
	 * 
	 * @param id
	 */
	public void outputCardData(String id) {
		List<RecordModel> list = rd.selectList(new RecordModel(null, null, null));
		if (list == null) {
			System.out.println("未找到借书卡里的信息");
			return;
		}
		boolean exist = false;
		for (RecordModel record : list) {
			if (record.getCId().equals(id)) {
				String type = decodeType(record.getType());
				System.out.println("书ID: " + record.getBId() + " 借书卡ID: " + record.getCId() + " 借书类型:" + type + " 日期:"
						+ record.getTimeDate());
				exist = true;
			}
		}
		if (exist) {
			System.out.println("借书卡Id输入有误或者该借书卡未过借书！！");
		}
	}

	/**
	 * 子功能 -- 解码图书借还类型
	 * 
	 * @param type
	 * @return
	 */
	public String decodeType(int type) {
		return type == 1 ? "借书" : "还书";
	}

	/**
	 * 显示图书列表（按借出次序）
	 */
	public void showBook() {
		List<BookModel> list = bd.selectOrderList(new BookModel(null, null, null, null));
		if (list == null) {
			System.out.println("未找到图书信息");
			return;
		}
		for (BookModel book : list) {
			System.out.println("图书ID: " + book.getId() + " 书名: " + book.getName() + " 图书数量: " + book.getNumber()
					+ " 借阅次数: " + book.getCount());
		}
	}
}
